package com.blueocean_health.care.management.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.UserPatient;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.OrderFlagEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.ordervo.UserOrderVo;
import com.blueocean_health.care.management.exception.OrderException;
import com.blueocean_health.care.management.service.OrderInfoService;

/**
 * @Author ChenPeng cp
 * @Date 2017/8/30 9:49
 */

@RestController
@RequestMapping(value = "/app/order/user", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderInfoUserController {

    @Autowired
    private OrderInfoService orderInfoService;

    @RequestMapping("/add")
    public SysResult insertUserOrder(@RequestBody UserOrderVo userOrderVo) throws OrderException {
        UserPatient val = LocalManager.getVal(LocalManagerType.USER_APP);
        String loginPhone = val.getPhone();
        Long version = userOrderVo.getVersion();
        return orderInfoService.insertUserOrder(userOrderVo,version,loginPhone);
    }


    @RequestMapping("/delete")
    public SysResult deleteUserOrder(@RequestBody String jsonString) throws OrderException {
        UserPatient val = LocalManager.getVal(LocalManagerType.USER_APP);
        if (StringUtils.isEmpty(val)){
            return SysResult.error("请先登录！");
        }
        String loginPhone = val.getPhone();
        JSONObject jsonObject =  JSONObject.parseObject(jsonString);
        String orderId = (String) jsonObject.get("orderId");
        return orderInfoService.deleteOrRecoveryOrderInfo(OrderFlagEnum._1.getKey(),orderId,loginPhone);
    }
}
